package group2assignone.ucm.administrator;

import group2assignone.dao.MyDAO;
import group2assignone.func.Authentication;
import group2assignone.model.Manager;
import group2assignone.ucm.UCController;
import group2assignone.ucm.UCStartup;
import group2assignone.view.UITool;
import group2assignone.view.administrator.AddNewManagerView;

/**
 * Use Case: Save New Manager                                              <br>
 * Pre-condition: User has clicked save from the  Edit Manager view.        <br>
 * Post-condition: UC Admin main view is run                                 <br>
 * 1. system retrievs the manager data                                      <br>
 * 2. system checks the fields are valid                                    <br>
 * 3. system closes the add manager view<br>
 * 4. system writes the new data to the database                            <br>
 * 5. system runs Use case admin main view                                     <br>
 *      2. a) Invalid manager data entered<br>
 *              - Display error<br>
 * @author group2                                                           <br>
 */
public class UCSaveNewManager implements UCController {

    private AddNewManagerView view;
    private boolean ok;
    private Authentication auth;
    private Manager man;

    public UCSaveNewManager(AddNewManagerView v) {
        view = v;
        auth = new Authentication();
        man = new Manager();
    }

    public void run() {
        //1. system retrievs the manager data
        view.upLoad(man);
        //2. system checks the fields are valid
        ok = auth.checkManagerFields(man);

        if (ok) {
            //3. system closes the add manager view
            view.dispose();
            //4. system writes the new data to the database
            MyDAO coms = UCStartup.getInstance().getDAO();
            coms.sQLAddManager(man);
            // 5. system runs Use case admin main view
            UCUIAdminMainView uc = UCUIAdminMainView.getInstance();
            uc.run();
        } else {
            //2. a) Invalid manager data entered
            //         - Display error
            UITool.displayError(auth.getErrorMsg(), view);
        }

    }
}
